// import Cookies from 'js-cookie'
import { ElMessage } from 'element-plus'

export default {

    // 颜色转换hex格式转rgba格式
    hexToRgba: (hex:any, opacity:any) => {
        return "rgba(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + "," + opacity + ")";
    },

    isInArr: (arr:any[], value:any) => {
        for(let i = 0; i<arr.length; i++){
            if(arr[i] instanceof Array){
                // @ts-ignore
                return this.isInArr(arr[i],value)
            }else{
                if(arr[i] == value){
                    return true //存在
                }
            }
        }

        return false   // 不存在
    },

    makeUrl: (url:string) : string => {
        if(url){
            return (url.includes('http://') || url.includes('https://') || url.includes('data:image')) ? url : import.meta.env.VITE_APP_HOST + url
        }
        return url

    },

    isEmpty:(data:any) => {
        
        // 空数据
        if(!data){
            return true
        }
        // 空数组
        if(Array.isArray(data) && data.length == 0){
            return true
        }
        
        // 空对象
        if(typeof data == "object"){
            let keys = Object.keys(data)
            if(keys.length == 0){
                return true
            }
        }
        
        
        return false
    },

    isNumber: (str:any) => {
        let regPos = /^\d+(\.\d+)?$/; //非负浮点数
        let regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
        if(regPos.test(str) || regNeg.test(str)) {
            return true;
        } else {
            return false;
        }
    },

    // 匹配价格的正则表达式
    priceReg: () => {
        return /(?!(^[0.]*$))((^[1-9]\d*$)|(?!(^0{2,}.))(?!(^0{1,}[1-9]+))(^[0-9]+.\d{1,2}$))/
    },
    // 深度复制对象
    deepVal: (val:object) => {
        if(!val){
            return val  // 将原变量返回
        }else{
            return JSON.parse(JSON.stringify(val))
        }
    }


}

